package org.cool.vnc.proto;

import org.jboss.netty.buffer.ChannelBuffer;

public class ProtocolVersion extends RFBMessage {
	private int major = 0;
	private int minor = 0;

	public ProtocolVersion(int major, int minor) {
		this.major = major;
		this.minor = minor;
	}

	public int getMajor() {
		return major;
	}

	public int getMinor() {
		return minor;
	}

	public void write(ChannelBuffer buf) {
		buf.writeBytes(toString().getBytes(DEFAULT_CHARSET));
	}

	public String toString() {
		return String.format("RFB %1$03d.%2$03d\n", major, minor);
	}

	public static ProtocolVersion parse(ChannelBuffer buf) {
		byte[] b = new byte[12];
		buf.readBytes(b);
		int major = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0');
		int minor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0');
		return new ProtocolVersion(major, minor);
	}

}